package cn.yoyo.dao;

import cn.yoyo.entity.Blog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class BlogDaoImpl implements BlogDao{
    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public List<Blog> findAllCanSee() {
        Query query = new Query(Criteria.where("audit").is(0));
        return mongoTemplate.find(query,Blog.class);
    }

    @Override
    public List<Blog> findAllCantSee() {
        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.orOperator(Criteria.where("audit").is(1),Criteria.where("audit").is(2));
        query.addCriteria(criteria);
        return mongoTemplate.find(query,Blog.class);
    }


    @Override
    public List<Blog> findAll() {
        return mongoTemplate.findAll(Blog.class);
    }

    @Override
    public List<Blog> findByType(String typeId) {
        Query query = new Query(Criteria.where("typeId").is(typeId));
        return mongoTemplate.find(query,Blog.class);
    }

    @Override
    public void update(Blog blog) {
        mongoTemplate.save(blog);
    }


    @Override
    public void updateSum(Map<String,Object> map) {
        Update update = new Update();
        Query query = new Query(Criteria.where("id").is(map.get("blogId")));
        if (map.get("pageviews")!=null){
            update.set("pageviews",map.get("pageviews"));
        }
        if (map.get("likes")!=null){
            update.set("likes",map.get("likes"));
        }
        if (map.get("favouriteSum")!=null){
            update.set("favouriteSum",map.get("favouriteSum"));
        }
        mongoTemplate.updateFirst(query,update,Blog.class);
    }

    @Override
    public List<Blog> findMyBlogs(String userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        return mongoTemplate.find(query,Blog.class);
    }

    @Override
    public Blog findById(String blogId) {
        Query query = new Query(Criteria.where("id").is(blogId));
        return mongoTemplate.findOne(query,Blog.class);
    }

    @Override
    public void add(Blog blog) {
        mongoTemplate.insert(blog);
    }

    @Override
    public void isPass(String blogId) {
        Update update = new Update();
        Query query = new Query(Criteria.where("id").is(blogId));
        update.set("audit",0);
        mongoTemplate.updateFirst(query,update,Blog.class);
    }

    @Override
    public void noPass(String blogId) {
        Update update = new Update();
        Query query = new Query(Criteria.where("id").is(blogId));
        update.set("audit",2);
        mongoTemplate.updateFirst(query,update,Blog.class);
    }

    @Override
    public void delete(String blogId) {
        Query query = new Query(Criteria.where("id").is(blogId));
        mongoTemplate.findAndRemove(query,Blog.class);
    }
}
